<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改测点')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-point-edit" th:object="${point}">
        <input id="pointId" name="pointId" type="hidden" th:field="*{id}"/>
        <div class="form-group">
            <label class="col-sm-3 control-label">风机：</label>
            <div class="col-sm-8">
                <select
                        class="form-control"
                        name="fanId"
                        id="fanId"
                        th:field="*{fanId}"
                        required
                >
                    <option value="" style="display: none">请选择</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">测项：</label>
            <div class="col-sm-8">
                <select
                        class="form-control"
                        name="functionId"
                        id="functionId"
                        th:field="*{functionId}"
                        required
                >
                    <option value="" style="display: none">请选择</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">测点名称：</label>
            <div class="col-sm-8">
                <input
                        class="form-control"
                        type="text"
                        name="pointName"
                        id="pointName"
                        th:field="*{pointName}"
                        required
                />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">标高</label>
            <div class="col-sm-8">
                <input
                        class="form-control"
                        type="text"
                        name="height"
                        id="height"
                        th:field="*{height}"
                        required
                />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">采集仪IP</label>
            <div class="col-sm-8">
                <input
                        class="form-control"
                        type="text"
                        name="pointName"
                        id="ip"
                        th:field="*{ip}"
                        required
                />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">端口</label>
            <div class="col-sm-8">
                <input
                        class="form-control"
                        type="text"
                        name="com"
                        id="com"
                        th:field="*{com}"
                        required
                />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">初始值1：</label>
            <div class="col-sm-8">
                <input
                        class="form-control"
                        type="text"
                        name="init1"
                        id="init1"
                        th:field="*{init1}"
                        required
                />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">初始值2：</label>
            <div class="col-sm-8">
                <input
                        class="form-control"
                        type="text"
                        name="init2"
                        id="init2"
                        th:field="*{init2}"
                        required
                />
            </div>
        </div>
      <div class="form-group">
        <label class="col-sm-3 control-label">初始值3：</label>
        <div class="col-sm-8">
          <input
                  class="form-control"
                  type="text"
                  name="init2"
                  id="init2"
                  th:field="*{init2}"
                  required
          />
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-3 control-label">初始值设置时间</label>
        <div class="col-sm-8">
          <input
                  class="form-control"
                  type="text"
                  name="initTime"
                  id="initTime"
                  th:field="*{initTime}"
                  required
          />
        </div>
      </div>
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label">预警值1：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <input-->
<!--                        class="form-control"-->
<!--                        type="text"-->
<!--                        name="overLine1"-->
<!--                        id="overLine1"-->
<!--                        th:field="*{overLine1}"-->
<!--                        required-->
<!--                />-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label">预警值2：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <input-->
<!--                        class="form-control"-->
<!--                        type="text"-->
<!--                        name="overLine2"-->
<!--                        id="overLine2"-->
<!--                        th:field="*{overLine2}"-->
<!--                        required-->
<!--                />-->
<!--            </div>-->
<!--        </div>-->
    </form>
</div>
<th:block th:include="include :: footer"/>
<script type="text/javascript">
    // layui 初始化
    layui.use('form', function () {
        var laydate = layui.laydate;

        //初始值设置时间
        laydate.render({
            elem: '#initTime'
            , type: 'datetime'
            , isInitValue: true
        });
    })


    var fandArr = [],
        functionArr = [],
        fanId = null,
        functionId = null;
    var prefix = ctx + "sensor/point";

    $("#form-point-edit").validate({
        onkeyup: false,
        rules: {
            pointName: {
                remote: {
                    url: ctx + "sensor/point/checkPointNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        pointId: function () {
                            return $("input[name='pointId']").val();
                        },
                        pointName: function () {
                            return $.common.trim($("#pointName").val());
                        },
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    },
                },
            },
        },
        messages: {
            pointName: {
                remote: "测点名称已经存在",
            },
        },
        focusCleanup: true,
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $("#form-point-edit").serialize());
        }
    }

    // 监听项目改变时
    $("[name='fanId']").on("change", function () {
        fanId = $(this).val();
        getCxData(fanId);
    });

    // 监听测项改变时
    $("[name='functionId']").on("change", function () {
        functionId = $(this).val();
    });

    // 获取项目数据
    function getPorjectData() {
        $.ajax({
            url: ctx + "sensor/commom/getFanListForSensor",
            Type: "post",
            dataType: "json",
            async: false,
            success: (data) => {
                fandArr = [...data];
                let str = "";
                data.forEach((item) => {
                    str += `<option value="${item.id}">${item.fanName}</option>`;
                });
                $("[name='fanId']").append(str);
            },
        });
    }

    // 获取测项数据
    function getCxData(fanId) {
        $.ajax({
            url: ctx + "sensor/commom/getFunctionListForFan",
            Type: "post",
            data: {
                fanId: fanId,
            },
            dataType: "json",
            async: false,
            success: (data) => {
                functionArr = [...data];
                let str = "";
                data.forEach((item) => {
                    str += `<option value="${item.id}">${item.name}</option>`;
                });
                $("[name='functionId']").find("option").slice(1).remove();
                $("[name='functionId']").append(str);
            },
        });
    }

    getPorjectData();
</script>
</body>
</html>
